﻿using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Windows
{
    public class App
    {
        public string ErrorEvent = "";

        System.Diagnostics.Process prc = new System.Diagnostics.Process();

        public void New(string path, string arg)
        {
            if (arg == null)
            {
                if (File.Exists(path) == true)
                {
                    prc.StartInfo.FileName = path;
                    prc.StartInfo.ErrorDialog = true;
                    try { prc.Start(); }
                    catch (InvalidCastException e) { ErrorEvent = Convert.ToString(e); }
                    catch (System.ComponentModel.Win32Exception e) { ErrorEvent = Convert.ToString(e); }
                    catch (ObjectDisposedException e) { ErrorEvent = Convert.ToString(e); }
                }
                else
                    ErrorEvent = "File Not Found ('" + path + "')";
            }
            else
            {
                if (File.Exists(path) == true)
                {
                    prc.StartInfo.FileName = path;
                    prc.StartInfo.ErrorDialog = true;
                    prc.StartInfo.Arguments = arg;
                    try { prc.Start(); }
                    catch (InvalidCastException e) { ErrorEvent = Convert.ToString(e); }
                    catch (System.ComponentModel.Win32Exception e) { ErrorEvent = Convert.ToString(e); }
                    catch (ObjectDisposedException e) { ErrorEvent = Convert.ToString(e); }
                }
            }
        }
        public void Kill(string process)
        {
            Process[] prcs = Process.GetProcessesByName(process);
            foreach (Process prc in prcs)
            {
                prc.Kill();
            }
        }

    }

    public class PC
    {
        string windir = Environment.GetEnvironmentVariable("WINDIR");
        System.Diagnostics.Process prc = new System.Diagnostics.Process();
        public void Abort_Shutdown()
        {
            prc.StartInfo.FileName = windir + @"\System32\Shutdown.exe";
            prc.StartInfo.Arguments = "-s";
            prc.Start();
        }
        public void ShutDown(bool force)
        {
            prc.StartInfo.FileName = windir + @"\System32\Shutdown.exe";
            if (force == true)
                prc.StartInfo.Arguments = "-s -f";
            else
                prc.StartInfo.Arguments = "-s";
            prc.Start();
        }
        public void Restart(bool force)
        {
            prc.StartInfo.FileName = windir + @"\System32\Shutdown.exe";
            if (force == true)
                prc.StartInfo.Arguments = "-r -f";
            else
                prc.StartInfo.Arguments = "-r";
            prc.Start();
        }
        public void Logoff(bool force)
        {
            prc.StartInfo.FileName = windir + @"\System32\Shutdown.exe";
            if (force == true)
                prc.StartInfo.Arguments = "-l -f";
            else
                prc.StartInfo.Arguments = "-l";
            prc.Start();
        }
    }
}
